package com.feib.soeasy.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
 * @title (#)Role.java<br>
 * @description 使用者角色<br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/7 create by Jimmy Liu<br>
 */
@Entity
@Table(name = "APP_ROLE", uniqueConstraints = { @UniqueConstraint(columnNames = { "ROLE_NO", "GROUP_PK" }) })
public class Role extends AuditEntity {

    private static final long serialVersionUID = 9187640952798010861L;

    private Long rolePk;

    /**
     * 所屬群組
     */
    private Group group;

    /**
     * 角色代碼
     */
    private String roleNo;

    /**
     * 角色名稱
     */
    private String roleName;

    /**
     * 備註
     */
    private String roleMemo;

    /**
     * 使用者
     */
    private Set<User> users;

    /**
     * 可使用的Menu
     */
    private Set<MenuItem> menuItems;


    /**
     * @return the rolePk
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ROLE_PK", nullable=false)
    public Long getRolePk() {
        return rolePk;
    }


    /**
     * @param rolePk the rolePk to set
     */
    public void setRolePk(Long rolePk) {
        this.rolePk = rolePk;
    }


    /**
     * @return the group
     */
    @ManyToOne
    @JoinColumn(name="GROUP_PK", nullable=false)
    public Group getGroup() {
        return group;
    }


    /**
     * @param group the group to set
     */
    public void setGroup(Group group) {
        this.group = group;
    }



    /**
     * @return the roleNo
     */
    @Column(name="ROLE_NO", nullable=false, length=20)
    public String getRoleNo() {
        return roleNo;
    }



    /**
     * @param roleNo the roleNo to set
     */
    public void setRoleNo(String roleNo) {
        this.roleNo = roleNo;
    }


    /**
     * @return the roleName
     */
    @Column(name="ROLE_NAME", nullable=false, length=50)
    public String getRoleName() {
        return roleName;
    }



    /**
     * @return the roleMemo
     */
    @Column(name="ROLE_MEMO", length=255)
    public String getRoleMemo() {
        return roleMemo;
    }



    /**
     * @param roleMemo the roleMemo to set
     */
    public void setRoleMemo(String roleMemo) {
        this.roleMemo = roleMemo;
    }


    /**
     * @param roleName the roleName to set
     */
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }


    /**
     * @return the menuItems
     */
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinTable(name="ROLE_MENU", joinColumns = @JoinColumn(name = "ROLE_PK"), inverseJoinColumns = @JoinColumn(name="MENU_ITEM_PK")) 
    public Set<MenuItem> getMenuItems() {
        return menuItems;
    }


    /**
     * @param menuItems the menuItems to set
     */
    public void setMenuItems(Set<MenuItem> menuItems) {
        this.menuItems = menuItems;
    }

    /**
     * @return 傳回 users。
     */
    @ManyToMany(mappedBy = "roles")
    public Set<User> getUsers()
    {
        return users;
    }


    /**
     * @param users 要設定的 users。
     */
    public void setUsers(Set<User> users)
    {
        this.users = users;
    }


    @Override
    public String toString()
    {
        return new ToStringBuilder(this).append("rolePk", rolePk).append("roleNo", roleNo).append("roleName", roleName).append("roleMemo", roleMemo).toString();
    }

    @Override
    public boolean equals(final Object other)
    {
        if (this == other)
            return true;
        if (!(other instanceof Role))
            return false;
        Role castOther = (Role) other;
        return new EqualsBuilder().append(rolePk, castOther.rolePk).isEquals();
    }

    @Override
    public int hashCode()
    {
        return new HashCodeBuilder().append(rolePk).toHashCode();
    }    
}
